<?php
// š
include_once('data.php');

if (isset($_GET['line']) && isset($_GET['stop'])) {
	$mysqli = new mysqli(CONNECT_HOST, CONNECT_USER, CONNECT_PASS, CONNECT_DB);
	$mysqli->query("SET NAMES 'utf8'");
	
	if ((date('w') == 0) || (date('w') ==6)) {
		$dayofweek = 2;
	} else {
		// tu treba este vyriesit prazdniny
		$dayofweek = 1;
	}
	
	$sql = "select line_id, line_direction, stop_id, departure_time, schedule_type, exceptional_route_id from linestopdepart where line_id in (".$_GET['line'].") and stop_id=".$_GET['stop']." and schedule_type=".$dayofweek." and departure_time > now() and departure_time < DATE_ADD(NOW(), INTERVAL 2 HOUR) order by departure_time;";
	$result = $mysqli->query($sql);
	if (mysqli_num_rows($result) != 0) {
		echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
		echo "<linedepartlist>\n";
		while ($row = $result->fetch_assoc()) {
				echo "\t<departure line_id=\"".$row["line_id"]."\" direction=\"".$row["line_direction"]."\" stop_id=\"".$row["stop_id"]."\">\n";
				echo "\t\t<departure_time>" . $row["departure_time"] . "</departure_time>\n";
				echo "\t\t<exceptional_route_id>" . $row["exceptional_route_id"] . "</exceptional_route_id>\n";
				echo "\t</departure>\n";
		}
		echo "</linedepartlist>";
	} else {
		echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<linedepartlist>\n</linedepartlist>";	
	}
}
?>